﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 流程经办
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-06-15
    /// </summary>
    internal class FlowTaskOperatorService : DataFactory, FlowTaskOperatorIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="taskId">流程实例Id</param>
        /// <returns></returns>
        public async Task<List<FlowTaskOperatorEntity>> GetList(string taskId)
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<FlowTaskOperatorEntity>().Where(m => m.TaskId == taskId).OrderBy(o => o.CreatorTime).ToListAsync();
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<FlowTaskOperatorEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<FlowTaskOperatorEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="taskId">实例Id</param>
        /// <param name="nodeNo">节点编码</param>
        /// <returns></returns>
        public async Task<FlowTaskOperatorEntity> GetInfo(string taskId, string nodeNo)
        {
            using (var db = this.Basic)
            {
                var userInfo = UserProvider.Instance.Get();
                return await db.GetEntityAsync<FlowTaskOperatorEntity>(m => m.TaskId == taskId && m.NodeCode == nodeNo && m.Completion == 0 && m.HandleId == userInfo.UserId);
            }
        }
        /// <summary>
        /// 删除（根据实例Id）
        /// </summary>
        /// <param name="taskId">任务主键</param>
        /// <returns></returns>
        public async Task<int> DeleteByTaskId(string taskId)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync<FlowTaskOperatorEntity>(m => m.TaskId == taskId);
            }
        }
        /// <summary>
        /// 删除（根据节点Id）
        /// </summary>
        /// <param name="nodeId">节点主键</param>
        /// <returns></returns>
        public async Task<int> DeleteByNodeId(string nodeId)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync<FlowTaskOperatorEntity>(m => m.TaskNodeId == nodeId && m.Completion == 0);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entitys">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(List<FlowTaskOperatorEntity> entitys)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entitys);
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(FlowTaskOperatorEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        public async Task<int> UpdateCompletion(string taskId, List<string> userId)
        {
            using (var db = this.Basic)
            {
                var parameters = new List<DbParameter>();
                StringBuilder dbSql = new StringBuilder();
                dbSql.Append(@"update flow_taskoperator set F_Completion = '1' where F_TaskId = @taskId and F_HandleId in ('@handleId')");
                parameters.Add(DataHelper.CreateParameter("@taskId", taskId));
                var handleId = string.Join("','", userId);
                parameters.Add(DataHelper.CreateParameter("@handleId", handleId));
                return await db.ExecuteSqlAsync(dbSql.ToString(), parameters.ToArray());
            }
        }
    }
}